package com.ldxiwxr.system.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ldxiwxr.system.entity.Role;
import com.ldxiwxr.system.entity.User;
import com.ldxiwxr.system.entity.UserRole;
import com.ldxiwxr.system.mapper.MenuMapper;
import com.ldxiwxr.system.mapper.RoleMapper;
import com.ldxiwxr.system.mapper.UserRoleMapper;
import com.ldxiwxr.system.service.IRoleService;
import com.ldxiwxr.system.utils.HandleUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

import static com.ldxiwxr.system.common.Constant.LOGIN_USER;

/**
 * 用户权限实现类
 * @author SuBanxia
 */
@Service
public class RoleServiceImpl implements IRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenuMapper menuMapper;

    /**
     * 获取用户可以访问路径
     * @return  List<String> 路径
     */
    @Override
    public List<String> getAccessPath() {
        //获取Session中的角色
        User user = (User) StpUtil.getSession().get(LOGIN_USER);
        //通过获取角色的id获取角色的权限
        LambdaQueryWrapper<UserRole> wrapper = Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, user.getId());
        UserRole userRole = userRoleMapper.selectOne(wrapper);
        Role role = roleMapper.selectById(userRole.getRoleId());
        //获取具体权限
        List<Integer> permissions = HandleUtils.getPermissionByString(role.getPermission());
        return menuMapper.selectAccessPath(permissions);
    }
}
